﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace PFMConfigurations
{

    public class TargetManager : ConfigurationManager<Target>
    {
        public TargetManager()
        {
            this.File = @"E:\代码库\PFM\PFMConfigurations\PFM-Config.XML";
        }
        public void Load()//从XML文件中读取
        {
            XmlTextReader myReader = new XmlTextReader(this.File);
            myReader.Read();
            List<Target> result = new List<Target>();
            Target aa = new Target();
            while (myReader.Read())
            {
                //如果是开始节点
                if (myReader.NodeType == XmlNodeType.Element)
                {
                    //通过myReader.Name得到节点名
                    string elementName = myReader.Name;
                    if (elementName == "Target")
                    {
                        aa = new Target();
                    }
                    //读取到Target元素 这时myReader.Read()读取到的内容为<Target>...</Target>
                    else if (elementName == "Id")
                    {
                        //读取到节点内文本内容
                        if (myReader.Read())
                        {
                            //通过myReader.Value获得文本内容
                            aa.Id = (new Guid(myReader.Value));

                        }
                    }
                    else if (elementName == "Filter")
                    {
                        if (myReader.Read())
                        {
                            aa.Filter = myReader.Value;

                        }
                    }
                    else if (elementName == "Location")
                    {
                        if (myReader.Read())
                        {
                            string a = @myReader.Value;
                            aa.Location = a;

                        }
                    }
                    else if (elementName == "Remove")
                    {
                        if (myReader.Read())
                        {
                            if (myReader.Value == "false")
                            {
                                aa.Remove = false;
                            }
                            else
                                aa.Remove = true;

                        }
                    }
                    else if (elementName == "string")
                    {
                        if (myReader.Read())
                        {
                            //handler.Add( myReader.Value);
                            aa.Handler.Add(myReader.Value);

                        }
                    }
                    else if (elementName == "Unti")
                    {
                        if (myReader.Read())
                        {
                            aa.Unit = myReader.Value;

                        }
                    }
                    else if (elementName == "Destination")
                    {
                        if (myReader.Read())
                        {
                            aa.Destination = myReader.Value;

                        }
                    }
                    else if (elementName == "Dir")
                    {
                        if (myReader.Read())
                        {
                            aa.Dir = myReader.Value;

                        }
                    }
                    else if (elementName == "Subdirectory")
                    {
                        if (myReader.Read())
                        {
                            if (myReader.Value == "false")
                            {
                                aa.Subdirectory = false;
                            }
                            else
                                aa.Subdirectory = true;

                        }
                    }
                    else if (elementName == "ConnectionString")
                    {
                        if (myReader.Read())
                        {
                            aa.ConnectionString = myReader.Value;

                        }
                    }
                    else if (elementName == "TableName")
                    {
                        if (myReader.Read())
                        {
                            aa.TableName = myReader.Value;

                        }
                    }
                }
                else if (myReader.NodeType == XmlNodeType.EndElement)
                {
                    string elementName = myReader.Name;
                    if (elementName == "Target")
                    {
                        result.Add(aa);

                    }

                }
            }
            this.Items = result;

        }


        public override Target getItem(object id)
        {
            if (id is Guid && Items != null)
                return Items.First(x => x.Id.Equals(id));
            else
                return base.getItem(id);
        }
    }
}
